package com.seocoo.worker.user;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.seocoo.worker.R;
import com.seocoo.worker.base.CommWebActivity;
import com.seocoo.worker.base.Const;
import com.seocoo.worker.base.ImageLoaderEngine;
import com.seocoo.worker.base.entity.EUserInfoBean;
import com.seocoo.worker.friend.FriendAppraiseActivity;
import com.seocoo.worker.home.BusEventTab;
import com.seocoo.worker.user.account.AddBankCarActivity;
import com.seocoo.worker.user.account.MyAccountHomeActivity;
import com.seocoo.worker.user.account.MyBankCarActivity;
import com.seocoo.worker.user.account.entity.EBankCar;
import com.seocoo.worker.user.account.model.CardModel;
import com.seocoo.worker.user.model.UserModel;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 功能描述:
 *
 * @author
 * @date 2020/2/16
 */
public class UserFragment extends MVVMFragment implements View.OnClickListener {

    private ImageView ivTop;
    private CircleImageView ivPhoto;
    private TextView tvName;
    private TextView tvPhone;
    private TextView tvNumber;
    private ImageView ivRight;
    private TextView tvFraction;
    private TextView tv;
    private TextView tvPower;
    private TextView tvUpgrade;
    private RecyclerView recyclerView;
    private View clAgress;
    private View viewLeft,viewRight;
    @Override
    public int getLayout() {
        return R.layout.user_center_fragment;
    }

    @Override
    public void initData(View view) {
        ivTop = (ImageView) view.findViewById(R.id.iv_top);
        ivPhoto = (CircleImageView) view.findViewById(R.id.iv_photo);
        tvName = (TextView) view.findViewById(R.id.tv_name);
        tvPhone = (TextView) view.findViewById(R.id.tv_phone);
        tvNumber = (TextView) view.findViewById(R.id.tv_number);
        ivRight = (ImageView) view.findViewById(R.id.iv_right);
        clAgress = view.findViewById(R.id.cl_agress);
        tvFraction = (TextView) view.findViewById(R.id.tv_fraction);
        tv = (TextView) view.findViewById(R.id.tv);
        tvPower = (TextView) view.findViewById(R.id.tv_power);
        tvUpgrade = (TextView) view.findViewById(R.id.tv_upgrade);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        viewLeft =  view.findViewById(R.id.viewLeft);
        viewRight =  view.findViewById(R.id.viewRight);

        ivPhoto.setOnClickListener(this);
        tvName.setOnClickListener(this);
        ivRight.setOnClickListener(this);
        tvFraction.setOnClickListener(this);
        tvPower.setOnClickListener(this);
        tvUpgrade.setOnClickListener(this);

        List<ItemAdapter.Item> list = new ArrayList<>(7);
        list.add(new ItemAdapter.Item(R.drawable.user_account_icon, "我的账户"));
        list.add(new ItemAdapter.Item(R.drawable.user_order_icon, "我的订单"));
        list.add(new ItemAdapter.Item(R.drawable.user_evaluate_icon, "我的评价"));

        list.add(new ItemAdapter.Item(R.drawable.user_bank_icon, "我的银行卡"));
        list.add(new ItemAdapter.Item(R.drawable.user_friend_icon, "邀请好友"));
        list.add(new ItemAdapter.Item(R.drawable.user_about_icon, "关于平台"));
        list.add(new ItemAdapter.Item(R.drawable.user_server_icon, "联系平台"));

        ItemAdapter adapter = new ItemAdapter(list);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        this.getViewModel(UserModel.class).getUserInfo();
    }

    @LiveDataMatch
    public void onUserInfo(EUserInfoBean infoBean) {
        if (infoBean == null) {
            return;
        }

        if (TextUtils.isEmpty(infoBean.getAvatar())) {
            ivPhoto.setImageResource(R.drawable.user_register);
        } else {
            new ImageLoaderEngine(infoBean.getAvatar()).error(R.drawable.user_register).placeholder(R.drawable.user_register).into(ivPhoto);
        }
        tvPower.setText(String.format("竞争力%s分", infoBean.getIntegral()));
        tvName.setText(infoBean.getNickName());


        tvFraction.setText(infoBean.getLevel() + "");

        String phoneNumber = infoBean.getPhoneNumber().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        tvPhone.setText(phoneNumber);

        List<ItemAdapter.Item> list = new ArrayList<>(7);
        list.add(new ItemAdapter.Item(R.drawable.user_account_icon, "我的账户"));
        list.add(new ItemAdapter.Item(R.drawable.user_order_icon, "我的订单"));
        tvNumber.setText("编号：" + infoBean.getManNo());

        if (infoBean.isStarClassLevel()) {
            //星级
            clAgress.setVisibility(View.VISIBLE);
            viewLeft.setVisibility(View.VISIBLE);
            viewRight.setVisibility(View.VISIBLE);

            //若此工匠星级水平为五颗星，则展示“申请成为金牌工匠”入口
            tvUpgrade.setVisibility(infoBean.getLevel() >= 5 ? View.VISIBLE : View.GONE);
            list.add(new ItemAdapter.Item(R.drawable.user_evaluate_icon, "我的评价"));
        } else {
            //金牌
            clAgress.setVisibility(View.GONE);
            viewLeft.setVisibility(View.GONE);
            viewRight.setVisibility(View.GONE);
        }

        list.add(new ItemAdapter.Item(R.drawable.user_bank_icon, "我的银行卡"));
        list.add(new ItemAdapter.Item(R.drawable.user_friend_icon, "邀请好友"));
        list.add(new ItemAdapter.Item(R.drawable.user_about_icon, "关于平台"));
        list.add(new ItemAdapter.Item(R.drawable.user_server_icon, "联系平台"));

        ItemAdapter adapter = new ItemAdapter(list);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            ItemAdapter.Item item = (ItemAdapter.Item) adapter1.getItem(position);
            assert item != null;
            switch (item.item) {
                case "我的账户": {
                    MyAccountHomeActivity.start(Objects.requireNonNull(getActivity()));
                    break;
                }
                case "我的订单": {
                    postEvent(new BusEventTab(0));
                    break;
                }
                case "我的评价": {
                    FriendAppraiseActivity.start(Objects.requireNonNull(getActivity()));
                    break;
                }
                case "我的银行卡": {
                    getViewModel(CardModel.class).queryCard();

                    break;
                }
                case "邀请好友": {
                    EvaluateFriendActivity.start(Objects.requireNonNull(getActivity()));
                    break;
                }
                case "关于平台": {
                    CommWebActivity.start(getActivity(),"关于平台", Const.API +"/dwm/abstractDesc.html");
                    break;
                }
                case "联系平台": {
                    getViewModel(UserModel.class).getCallPhone(getContext());
                    break;
                }
                default:
                    break;
            }
        });
    }


    @LiveDataMatch
    public void onBankCar(EBankCar eBankCar) {
        if (eBankCar != null && !TextUtils.isEmpty(eBankCar.cardId)) {
            MyBankCarActivity.start(getActivity(), 1000);
        }else{
            AddBankCarActivity.start(getActivity(),1001);
        }
    }

    @Override
    public void onClick(View v) {
        if (ivPhoto == v || v == tvName || v == ivRight) {
            //个人信息
            UserInfoActivity.start(getContext());
        } else if (v == tvFraction || v == tvPower) {
            //我的等级
            UserGradeActivity.start(getContext());
        } else if (v == tvUpgrade) {
            //申请成为金牌工匠
            ApplyGoldActivity.start(getActivity());
        }
    }
}
